//时间转换(js将 “2021-07-06T06:23:57.000+00:00” 转换为年月日时分秒)
function transformTimestamp(timestamp) {
	let a = new Date(timestamp).getTime();
	const date = new Date(a);
	const Y = date.getFullYear() + '-';
	const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '  ';
	const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
	const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
	// const s = date.getSeconds(); // 秒
	const dateString = Y + M + D + h + m;
	// console.log('dateString', dateString); // > dateString 2021-07-06 14:23
	return dateString;
}







$(function(){
	//表单提交事件
	$('#multipleUploadForm').submit(function(event) {

		//var formElement = this;
		var formData = new FormData();
		var file = document.getElementById("multipleFileUploadInput").files[0];
		formData.append("files", file);

        var reason=$("#reason").val();
		var description=$("#description").val();
		var start=transformTimestamp($("#start").val());
		var end=transformTimestamp($("#end").val());
		//var start="2022-07-14 10:00:00";
		//var end="2022-07-19 10:00:00";
		//json部分
		var form = JSON.stringify({
			"reason": reason,
			"description": description,
			"start":start,
			"end":end
		});
		formData.append('form', new Blob([form], {type: "application/json"}));
		$.ajax({
			type: "POST",
			//enctype: 'multipart/form-data',
			url: "/leave/insert",
			data: formData,
			processData: false,
			contentType: false,
			dataType: "json",
			success: function (data) {
				$('#multipleFileUploadError').hide();
				alert("文件上传成功！");
				window.location.reload();
			},
			error: function (error) {
				console.log(error);
				$("#multipleFileUploadSuccess").hide();
				$("#multipleFileUploadError").html((error && error.message) || "Some Error Occurred");

				alert("文件上传失败！");
			}
		});

		event.preventDefault();
	});

	//查看文件列表
	$("#viewList").click(function(){
		$.ajax({
			type: "GET",
			data:{},
			url: "/file/getAppIndexImg",
			success: function (data) {
				var src=data.path;
				//var src="http://localhost:8080/image/logo-name.png"
				console.info(src);
				$("#img").append("<img src=\""+src+"\">");
            alert("success")
			},
			error: function (error) {
             alert("error")
			}
		});

	});
});


